#include "utilities.h"

int global_error;

/* combine name and needToAdd */
static char *makeName(char *name, char *needToAdd);

/* change to error mode */
void do_error(char *st, int line)
{
	fprintf(stderr,"line: %d, %s\n", line, st);
	global_error = ERROR;
}

/* change to error mode */
void do_lable_error(char *lable)
{
	fprintf(stderr,"no such lable: %s\n", lable);
	global_error = ERROR;
}

/* change to error mode */
void do_ent_error(char *lable, int line)
{
	fprintf(stderr,"line: %d, no such lable: %s\n",line, lable);
	global_error = ERROR;
}

/* change to un error mode */
void undo_error()
{
	global_error = 0;
}

/* check if error mode */
int check_error()
{
	if(global_error == ERROR)
		return true;
	return false;
}

/* open file wich the name "name" and end wich needToAdd */
FILE *openFile(char *name,char *needToAdd, char *mode)
{
	char *filename;
	FILE *r;
	
	filename = makeName(name,needToAdd);
	
	r = fopen(filename,mode);
	free(filename);
	
	return r;
}

/* remove the file */
void removeFile(char *name, char *needToAdd)
{
	char *filename = makeName(name,needToAdd);
	remove(filename);
	free(filename);
}

/* combine name and needToAdd */
static char *makeName(char *name, char *needToAdd)
{
	int i=0;
	int length1;
	int length2;
	char *filename;
	
	length1 = strlen(name);
	length2 = strlen(needToAdd);
	filename = (char *) calloc(length1 + length2 + 1,sizeof(char));
	
	while(i<length1)
	{
		filename[i] = name[i];
		i++;
	}
	while(i< length1+length2)
	{
		filename[i] = needToAdd[i-length1];
		i++;
	}
	filename[i] = '\0';
	return filename;
}

/* return a array at length "outLineLength" that represent "n" in binary  */
char *toCharArray(int n)
{
	char *array;
	int i = outLineLength - 1;
	
	array = calloc(outLineLength,sizeof(char));
	while(0<=i)
	{
		array[i] = (n%2) + '0';
		n=n/2;
		i--;
	}
	return array;
}


/* print line at base 4
 * line: binary line at length "outLineLength" that need to be printed
 * fp: the file that line will be printed at */
void printLineAt4Base(char *line, FILE *fp)
{
	int i=0;
	int num;
	char toPrint;
	while(i*2<outLineLength)
	{
		num = (line[i*2] - '0')*2;
		num += line[i*2 +1] - '0';
		toPrint = (char)(num + '0');
		fputc(toPrint,fp);
		i++;
	}
}

/*
 * check if string is legal number
 */
int is_single_number(const char *p)
{
	char c;
	int first_num_flag = 0;
	int spase_after_digit_flag = 0;
	
	while(isspace(*p) && *p != '\0')
		p++;
	if(*p == '\0')
		return false;
	if(*p == '-' || *p == '+')
		p++;

	if (*p)
	{
		while ((c=*p++))
		{
			if (isdigit(c))
			{
				if(spase_after_digit_flag)
				{
					return false;			/* found second number - error */
				}

				first_num_flag = 1;
			}
			else if(isspace(c))
			{
				if(first_num_flag)
				{
					spase_after_digit_flag = 1;
				}
			}
			else
			{
				return false;
			}
		}

		if(first_num_flag)
		{
			return true;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return true;
	}
}
